using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CodRevit.Services;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Collections.Generic;

namespace CodRevit.ViewModels
{
    /// <summary>
    /// 模拟选择视图模型
    /// </summary>
    public partial class SimulateSelectViewModel : ObservableObject
    {
        private readonly ISimulateSelectService _simulateSelectService;
        private readonly UIDocument _uidoc;
        private readonly List<Element> _elements;

        [ObservableProperty]
        private int elementCount;

        [ObservableProperty]
        private double simulationTime = 2.0;

        public SimulateSelectViewModel(ISimulateSelectService simulateSelectService, UIDocument uidoc, List<Element> elements)
        {
            _simulateSelectService = simulateSelectService;
            _uidoc = uidoc;
            _elements = elements;
            ElementCount = elements?.Count ?? 0;
        }

        [RelayCommand]
        private async void Ok()
        {
            // 关闭窗口
            RequestClose?.Invoke(true);
            
            // 在后台执行模拟选择，传入用户设置的时间
            await _simulateSelectService.StartSimulationAsync(_uidoc, _elements, SimulationTime);
        }

        [RelayCommand]
        private void Cancel()
        {
            _simulateSelectService.CancelSimulation();
            RequestClose?.Invoke(false);
        }

        /// <summary>
        /// 请求关闭窗口的事件
        /// </summary>
        public System.Action<bool> RequestClose { get; set; }
    }
}